package net.butfly.bus.server.invoker.impl;

import java.util.ArrayList;
import java.util.List;

import net.butfly.bus.server.config.detail.SpringConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class SpringProvider extends ProviderBase {
	private static Logger logger = LoggerFactory.getLogger(SpringProvider.class);

	private SpringConfig config;

	private static SpringProvider instance = new SpringProvider();

	public static SpringProvider getInstance(Object config) {
		instance.config = (SpringConfig) config;
		return instance;
	}

	private SpringProvider() {

	}

	@Override
	protected List<Object> getBeanList() {
		List<Object> beans = null;
		String files = config.getFiles();
		logger.info("Spring分析业务逻辑开始.");
		logger.info("尝试分析以下配置文件:" + files + ".");
		String[] springFiles = files.split(";");

		for (String springFile : springFiles) {
			logger.info("开始尝试分析配置文件:" + springFile + ".");
			ClassPathXmlApplicationContext beanFactory = new ClassPathXmlApplicationContext(springFile);
			String[] beanAry = beanFactory.getBeanNamesForType(Object.class);
			if (beanAry != null) {
				beans = beans != null ? beans : new ArrayList<Object>();
				for (String bn : beanAry) {
					beans.add(beanFactory.getBean(bn));
				}
			}
			logger.info("Spring分析配置文件:" + springFile + "完成.");
		}
		return beans;
	}
}
